package com.printeverywhere.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import paypal.payflow.Invoice;

import com.opensymphony.xwork2.ActionContext;
import com.printeverywhere.delegator.AuctionBidsServiceDelegator;
import com.printeverywhere.delegator.AuctionServiceDelegator;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.delegator.FeedbackServiceDelegator;
import com.printeverywhere.delegator.ItemsServiceDelegator;
import com.printeverywhere.delegator.ItemsWatchListServiceDelegator;
import com.printeverywhere.delegator.MailServerDelegator;
import com.printeverywhere.delegator.TransactionsServiceDelegator;
import com.printeverywhere.delegator.UserServiceDelegator;
import com.printeverywhere.dto.AuctionBids;
import com.printeverywhere.dto.AuctionInvoices;
import com.printeverywhere.dto.AuctionLostlist;
import com.printeverywhere.dto.AuctionWonlist;
import com.printeverywhere.dto.Feedback;
import com.printeverywhere.dto.ItemNote;
import com.printeverywhere.dto.ItemOffers;
import com.printeverywhere.dto.Items;
import com.printeverywhere.dto.ItemsWatchlist;
import com.printeverywhere.dto.ListingOptions;
import com.printeverywhere.dto.MailServer;
import com.printeverywhere.dto.MailingSystem;
import com.printeverywhere.dto.Transactions;
import com.printeverywhere.dto.User;
import com.printeverywhere.dto.UserFavoritesSearch;
import com.printeverywhere.dto.UserRating;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.AttributeConstants;
import com.printeverywhere.util.DateUtil;
import com.printeverywhere.util.MailHelper;
import com.printeverywhere.util.SearchCriteria;
import com.printeverywhere.util.SearchListCriteria;
import com.printeverywhere.util.SessionUtil;

public class BuyingAction extends FrontBaseSupport implements ServletRequestAware {

	/**
	 *
	 */

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(BuyingAction.class);
	private SearchListCriteria searchListCriteria = new SearchListCriteria();
	private List<ItemsWatchlist> itemsWatchListTemp = new ArrayList<ItemsWatchlist>();
	private List<AuctionBids> auctionBidsList = new ArrayList<AuctionBids>();
	private List<Items> itemsList = new ArrayList<Items>();
	private List<Items> compareItemsList = new ArrayList<Items>();
	private ListingOptions listingOptions;
	private SearchCriteria searchCriteria = new SearchCriteria();
	private HttpServletRequest request;
	private User user;
	private MailServer mailContent = new MailServer();
	private AuctionInvoices invoice;
	private String action;
	private List<ItemOffers> itemOffersTemp;
	private String redirectAction;

	MyAccountAction accountAction = new MyAccountAction();

	public String getRedirectAction() {
		return redirectAction;
	}

	public void setRedirectAction(String redirectAction) {
		this.redirectAction = redirectAction;
	}

	public BuyingAction() {
		super(true, false);
		setMyAccount(true);
	}

	public String execute(){
		try {
			getBuyingWatchingData();
		} catch (Exception e) {
			return INPUT;
		}
		return SUCCESS;
	}

	public String buyBidding(){
		try {
			getBuyingBiddingData();
		} catch (Exception e) {
			addActionError("Problem in retriving buyBidding data.");
		}
		return SUCCESS;
	}

	public void buyingOverviewPurchased(HttpServletRequest request, List<Items> itemsList){
		this.request = request;
		this.itemsList = itemsList;
		buyingPurchased();
	}

	public void buyingOverviewWatching(HttpServletRequest request, List<Items> itemsWatchList){
		this.request = request;
		this.itemsList = itemsWatchList;
		getBuyingWatchingData();
	}

	public String buyingPurchased(){
		if(itemsList.size() > 0){
			itemsList.clear();
		}
		user = SessionUtil.getUser(request);
		listingOptions = (ListingOptions) ActionContext.getContext().getApplication().get(AdminConstants.LISTING_OPTIONS);

		searchCriteria.removeAllAttribute();
		searchCriteria.setAttribute(AdminConstants.USER, user);
		try{
			List<AuctionWonlist> list = (List<AuctionWonlist>) CommonFunctionDelegator.GeneralgetCriteriaList(AuctionWonlist.class.getName(), searchCriteria);
			for (int i=0;i<list.size();i++) {
				if(list.get(i).getItems().getItemtype().getId() == 4){
					AuctionBids auctionBids = new AuctionBids();
					AuctionWonlist auctionWonlist = list.get(i);
					Items items = auctionWonlist.getItems();
					String[] giftIcon;
					giftIcon = items.getGiftIcon().split(AdminConstants.SEMICOLON);
					if(giftIcon.length > 0)
						items.setGiftIcon(giftIcon[0]);
					auctionBids = AuctionBidsServiceDelegator.getMaxBidder(items.getItemId());
					if(auctionWonlist.getType() == 1){
						items.setType("Auctioned");
					}
					if(auctionWonlist.getType() == 2){
						items.setType("Bought");
					}
					if(auctionWonlist.getType() == 3){
						items.setType("Accepted offer");
					}
					if(auctionBids != null){
						items.setBidder(auctionBids.getUser());
						items.setWinningBid(auctionBids);
					}else{
						items.setBidder(new User());
						items.setWinningBid(auctionBids);
					}
					if(items.getEndTime().before(DateUtil.getTodaysDate())){
						items.setClosed(true);
					}else{
						items.setClosed(false);
					}
					if(items.getStartTime().before(DateUtil.addDate(items.getStartTime(), Calendar.DATE, listingOptions.getNumberOfDaysForNewListings()))){
						items.setNewIcon(true);
					}else{
						items.setNewIcon(false);
					}
					invoice = getInvoice(items, user);
					if(invoice != null){
						items.setInvoice(invoice);
					}
					if(getRating(items.getUserId().getId()) != null){
						items.setSellerRating(getRating(items.getUserId().getId()));
					}
					items.setRated(isRated(items, user));
					itemsList.add(items);
				}
			}
		}catch (Exception e) {
			logger.error(e.getMessage());
			addActionError("Error while getting list for My wanted Sold items");
		}

		searchListCriteria.removeAllAttribute();
		searchListCriteria.setAttribute("user", new Object[]{user, SearchListCriteria.NE});
		try{
			List<AuctionWonlist> list = AuctionServiceDelegator.getAuctionWonlistListBySearchListCriteria(searchListCriteria);
			for (int i=0;i<list.size();i++) {
				if((list.get(i).getItems().getItemtype().getId() != 4)){
					AuctionBids auctionBids = new AuctionBids();
					Items items = list.get(i).getItems();
					String[] giftIcon;
					giftIcon = items.getGiftIcon().split(AdminConstants.SEMICOLON);
					if(giftIcon.length > 0)
						items.setGiftIcon(giftIcon[0]);
					auctionBids = AuctionBidsServiceDelegator.getMaxBidder(items.getItemId());
					if(auctionBids != null){
						items.setBidder(auctionBids.getUser());
						items.setWinningBid(auctionBids);
					}else{
						items.setBidder(new User());
						items.setWinningBid(auctionBids);
					}
					if(items.getEndTime().before(DateUtil.getTodaysDate())){
						items.setClosed(true);
					}else{
						items.setClosed(false);
					}
					invoice = getInvoice(items, user);
					if(invoice != null){
						items.setInvoice(invoice);
					}
					items.setRated(isRated(items, user));
					itemsList.add(items);
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}

		return SUCCESS;
	}

	public String markAsPayed(){
		try {
			if(request.getParameter("invoiceId") != null) {
				String invoiceId = request.getParameter("invoiceId");
				searchCriteria.removeAllAttribute();
				searchCriteria.setAttribute("invoice", invoiceId);
				invoice = (AuctionInvoices) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, AuctionInvoices.class).get(0);
				invoice.setPaidDate(DateUtil.getTodaysDate());
				CommonFunctionDelegator.generalUpdatebyobj(invoice);
			}
		} catch (Exception e) {
			addActionError("Not Marked as Payed");
		}
		return SUCCESS;
	}

	public String askForInvoice(){
		String subject = "Ask For Invoice";
		int itemId;
		Items item;
		try{
			if((request.getParameter("itemId") != null) && (request.getParameter("action") != null)) {
				action = request.getParameter("action");
				itemId = Integer.parseInt(request.getParameter("itemId"));
				item = ItemsServiceDelegator.getItemsById(itemId);
				user = SessionUtil.getUser(request);
				User toUser = item.getUserId();
				this.mailContent.setUserByUserId(toUser);
				this.mailContent.setUserByFromId(user);
				this.mailContent.setBody("");
				this.mailContent.setSubject(subject);
				this.mailContent.setFlag(false);
				this.mailContent.setIsRead(false);
				DateFormat myDate = new SimpleDateFormat("yyyy-MM-dd hh:ss:mm");
				Calendar cal = GregorianCalendar.getInstance();
				cal.add(Calendar.MONDAY, 2);
				String strDate = myDate.format(cal.getTime());
				Date expiresDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:ss:mm").parse(strDate);
				this.mailContent.setExpiresDate(expiresDateFormat);
				MailingSystem mailingSystem = (MailingSystem) ActionContext.getContext().getApplication().get(AdminConstants.MAILING_OPTIONS);
				MailHelper mailHelper = new MailHelper(mailingSystem.getServerAddress(), mailingSystem.getServerUsername(), mailingSystem.getServerPassword());
				mailHelper.sendMail(mailingSystem.getWebmasterEmailAddress(), item.getUserId().getEmail() , subject , AdminConstants.BLANK);
				MailServerDelegator.insertMail(mailContent);
			}
		}catch (Exception e) {
			addActionError("Mail can not be sent.");
			return INPUT;
		}
		return SUCCESS;
	}

	public String buyingLost(){
		try {
			if(itemsList.size() > 0){
				itemsList.clear();
			}
			user = SessionUtil.getUser(request);
			listingOptions = (ListingOptions) ActionContext.getContext().getApplication().get(AdminConstants.LISTING_OPTIONS);

			searchCriteria.removeAllAttribute();
			searchCriteria.setAttribute(AdminConstants.USER, user);

			List<AuctionLostlist> auctionLostList = new ArrayList<AuctionLostlist>();
			auctionLostList = (List<AuctionLostlist>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, AuctionLostlist.class);
			for (Iterator iterator = auctionLostList.iterator(); iterator
					.hasNext();) {
				AuctionLostlist auctionLostlist = (AuctionLostlist) iterator
						.next();
				AuctionBids auctionBids = new AuctionBids();
				Items items = auctionLostlist.getItems();
				String[] giftIcon;
				giftIcon = items.getGiftIcon().split(AdminConstants.SEMICOLON);
				if(giftIcon.length > 0)
					items.setGiftIcon(giftIcon[0]);
				auctionBids = AuctionBidsServiceDelegator.getMaxBidder(items.getItemId());

				if(auctionBids != null){
					items.setBidder(auctionBids.getUser());
					items.setWinningBid(auctionBids);
				}else{
					items.setBidder(new User());
					items.setWinningBid(auctionBids);
				}
				if(items.getEndTime().before(DateUtil.getTodaysDate())){
					items.setClosed(true);
				}else{
					items.setClosed(false);
				}
				if(items.getStartTime().before(DateUtil.addDate(items.getStartTime(), Calendar.DATE, listingOptions.getNumberOfDaysForNewListings()))){
					items.setNewIcon(true);
				}else{
					items.setNewIcon(false);
				}
				items.setRated(isRated(items, user));
				if(getRating(items.getUserId().getId()) != null){
					items.setSellerRating(getRating(items.getUserId().getId()));
				}
				itemsList.add(items);
			}
		} catch (Exception e) {
			return INPUT;
		}
		return SUCCESS;
	}

	public String deleteLostItem(){
		try {
			if(request.getParameter("action") != null){
				action = request.getParameter("action");
				if (request.getParameter("t") != null) {
					action = action.concat("?t="+request.getParameter("t"));
				}
			}
			if((request.getParameter("itemId") != null) && (request.getParameter("deleteItem") != null)){
				Items item = ItemsServiceDelegator.getItemsById(Integer.parseInt(request.getParameter("itemId")));
				user = SessionUtil.getUser(request);
				searchCriteria.removeAllAttribute();
				searchCriteria.setAttribute("items", item);
				searchCriteria.setAttribute(AdminConstants.USER, user);
				AuctionLostlist auctionLostlist = (AuctionLostlist) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, AuctionLostlist.class).get(0);
				if(auctionLostlist != null)
				CommonFunctionDelegator.GeneralDelete(auctionLostlist);
				logger.info("Item deleted successfully.");
			}
		} catch (Exception e) {
			logger.error("Item not deleted successfully.");
			return INPUT;
		}
		return SUCCESS;
	}

	public String myOffers(){
		try {
			user = SessionUtil.getUser(request);
			listingOptions = (ListingOptions) ActionContext.getContext().getApplication().get(AdminConstants.LISTING_OPTIONS);
			searchCriteria.removeAllAttribute();
			searchCriteria.setAttribute("user.id", user.getId());
			searchCriteria.setAttribute("status.id", 4);
			itemOffersTemp = (List<ItemOffers>) CommonFunctionDelegator.GeneralgetCriteriaList(ItemOffers.class.getName(), searchCriteria);
			for (Iterator<ItemOffers> iterator = itemOffersTemp.iterator(); iterator
					.hasNext();) {
				ItemOffers itemOffers = (ItemOffers) iterator.next();
				Items items = itemOffers.getItems();
				if(items.getEndTime().before(DateUtil.getTodaysDate())){
					items.setClosed(true);
				}else{
					items.setClosed(false);
				}
				if(getRating(items.getUserId().getId()) != null){
					items.setSellerRating(getRating(items.getUserId().getId()));
				}
				itemOffers.setItems(items);
			}
		} catch (Exception e) {

		}
		return SUCCESS;
	}

	public String deleteMyOffer(){
		try {
			if(request.getParameter("cid") != null){
				int id = Integer.parseInt(request.getParameter("cid"));
				ItemOffers itemOffers = (ItemOffers) CommonFunctionDelegator.getObjectById(AdminConstants.ID, id, ItemOffers.class);
				CommonFunctionDelegator.GeneralDelete(itemOffers);
			}
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
			return INPUT;
		}
		return SUCCESS;
	}
	public String addAsFavoriteSearch(){
		try {
			user = SessionUtil.getUser(request);
			if((request.getParameter("itemId") != null) && (request.getParameter("action") != null)){
				action = request.getParameter("action");
				Items items = ItemsServiceDelegator.getItemsById(Integer.parseInt(request.getParameter("itemId")));
				UserFavoritesSearch userFavoritesSearch = new UserFavoritesSearch();
				userFavoritesSearch.setUser(user);
				User maxBidder = AuctionBidsServiceDelegator.getMaxBidder(items.getItemId()) != null ? AuctionBidsServiceDelegator.getMaxBidder(items.getItemId()).getUser(): null;
				if(maxBidder != null) {
					userFavoritesSearch.setBidder(maxBidder.getUserId());
				}
//				userFavoritesSearch.setBuyFrom()
//				userFavoritesSearch.setBuyTo(buyTo)
//				userFavoritesSearch.setCategoryId(items.getGeneralCategories());
				userFavoritesSearch.setCurrencyId(items.getCurrency().getId());
				userFavoritesSearch.setDate(DateUtil.getTodaysDate());
				userFavoritesSearch.setItems(items);
				userFavoritesSearch.setLanguage(user.getLanguages().getLanguageName());
				userFavoritesSearch.setLocation(items.getLocationCategoriesByState().getCategoryName());
				userFavoritesSearch.setName(items.getTitle());
				userFavoritesSearch.setQuantity(items.getQuantity());
				userFavoritesSearch.setRegionId(items.getLocationCategoriesByRegion());
				userFavoritesSearch.setSeller(items.getUserId().getUserId());
				userFavoritesSearch.setType(items.getItemtype());
				CommonFunctionDelegator.generalInsertbyobj(userFavoritesSearch);
			}
		} catch (Exception e) {
			addActionError("Item not added as favorite search.");
			logger.error(e.getMessage());
			return INPUT;
		}
		return SUCCESS;
	}

	public String deleteOrCompareItems(){
		try {
			if(request.getParameter("check_list") == null) {
				addActionError("Please select item for perform action");
				return INPUT;
			}
			if(request.getParameter("check_list") != null) {
				String[] checkedId = request.getParameterValues("check_list");
				if(request.getParameter("compare") != null && request.getParameter("compare").equalsIgnoreCase("Compare")){
					for (String chkid : checkedId) {
						Items item = ItemsServiceDelegator.getItemsById(Integer.parseInt(chkid));
						AuctionBids auctionBids = new AuctionBids();
						if(item!=null){
							String[] giftIcon;
							giftIcon = item.getGiftIcon().split(AdminConstants.SEMICOLON);
							if(giftIcon.length > 0)
								item.setGiftIcon(giftIcon[0]);
							auctionBids = AuctionBidsServiceDelegator.getMaxBidder(item.getItemId());
							if(auctionBids != null){
								item.setBidder(auctionBids.getUser());
								item.setWinningBid(auctionBids);
							}else{
								item.setBidder(new User());
								item.setWinningBid(auctionBids);
							}
							if(item.getEndTime().before(DateUtil.getTodaysDate())){
								item.setClosed(true);
							}else{
								item.setClosed(false);
							}
							compareItemsList.add(item);
						}
					}
					return "compareItems";
				}else if(request.getParameter("deleteMsg") != null && request.getParameter("deleteMsg").equalsIgnoreCase("delete")){
					ItemsServiceDelegator.deleteItems(request);
					addActionMessage("Items deleted successfully.");
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			addActionError("Error While deleting Items.");
			return INPUT;
		}finally{
			if(redirectAction != null && redirectAction.equalsIgnoreCase("buyWatching")){
				getBuyingWatchingData();
			}
		}
		return SUCCESS;
	}

	public AuctionInvoices getInvoice(Items items, User user){
		try{
			searchListCriteria.removeAllAttribute();
			searchListCriteria.setAttribute(AdminConstants.ID, new Object[]{items, SearchListCriteria.EQ});
			searchListCriteria.setAttribute(AdminConstants.USER,new Object[]{user, SearchListCriteria.EQ});
			if(TransactionsServiceDelegator.getList(searchListCriteria).size() > 0 ){
				invoice = new AuctionInvoices();
				Transactions transactions = TransactionsServiceDelegator.getList(searchListCriteria).get(0);
				invoice = transactions.getInvoice();
				return invoice;
			}
		}catch (Exception e) {
			return null;
		}
		return null;
	}

	public boolean isRated(Items items, User user){
		try{
			searchCriteria.removeAllAttribute();
			searchCriteria.setAttribute("userByToUserId", user);
			searchCriteria.setAttribute("itemId", items);
			if(CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, Feedback.class).size() > 0){
				return true;
			}else{
				return false;
			}
		}catch (Exception e) {
			return false;
		}
	}
	public void getBuyingWatchingData(){
		try {
			if(itemsList.size() > 0){
				itemsList.clear();
			}
			if(itemsWatchListTemp.size() > 0){
				itemsWatchListTemp.clear();
			}
			user = SessionUtil.getUser(request);
			listingOptions = (ListingOptions) ActionContext.getContext().getApplication().get(AdminConstants.LISTING_OPTIONS);
			searchListCriteria.removeAllAttribute();
			searchListCriteria.setAttribute(AttributeConstants.USER, new Object[]{user, SearchListCriteria.EQ});
			itemsWatchListTemp = ItemsWatchListServiceDelegator.getList(searchListCriteria);
			for (Iterator<ItemsWatchlist> iterator = itemsWatchListTemp.iterator(); iterator.hasNext();) {
				ItemsWatchlist type = (ItemsWatchlist) iterator.next();
				Items items = new Items();
				items = type.getItems();
				AuctionBids auctionBids = new AuctionBids();
				ItemNote itemNote = new ItemNote();
				String[] giftIcon;
				giftIcon = items.getGiftIcon().split(AdminConstants.SEMICOLON);
				if(giftIcon.length > 0)
					items.setGiftIcon(giftIcon[0]);
				if((items.getQuantity() != null) && (items.getSold() != null))
					items.setAvailable(items.getQuantity() - items.getSold());
				auctionBids = AuctionBidsServiceDelegator.getMaxBidder(items.getItemId());
				if(auctionBids != null){
					items.setBidder(auctionBids.getUser());
					items.setWinningBid(auctionBids);
					searchCriteria.removeAllAttribute();
					searchCriteria.setAttribute("ratedUser.id", items.getBidder().getId());
					if(getRating(items.getBidder().getId()) != null){
						items.setUserRating(getRating(items.getBidder().getId()));
					}
				}else{
					items.setBidder(new User());
					items.setWinningBid(auctionBids);
				}
				if(getRating(items.getUserId().getId()) != null){
					items.setSellerRating(getRating(items.getUserId().getId()));
				}
				if(items.getEndTime().before(DateUtil.getTodaysDate())){
					items.setClosed(true);
				}else{
					items.setClosed(false);
				}
				if(items.getStartTime().before(DateUtil.addDate(items.getStartTime(), Calendar.DATE, listingOptions.getNumberOfDaysForNewListings()))){
					items.setNewIcon(true);
				}else{
					items.setNewIcon(false);
				}
				/* retrieving note */
				searchCriteria.removeAllAttribute();
				searchCriteria.setAttribute(AttributeConstants.ITEMS, items);
				searchCriteria.setAttribute(AttributeConstants.USER, user);
				itemNote = (ItemNote) ((CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemNote.class)).size() > 0 ? CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemNote.class).get(AdminConstants.ZERO_NUMBER) : new ItemNote());
				items.setItemNote(itemNote);
				itemsList.add(items);
			}
		} catch (Exception e) {

		}
	}

	public void getBuyingBiddingData(){
		try {
			if(itemsList.size() > 0){
				itemsList.clear();
			}
			if(auctionBidsList.size() > 0){
				auctionBidsList.clear();
			}
			user = SessionUtil.getUser(request);
			listingOptions = (ListingOptions) ActionContext.getContext().getApplication().get(AdminConstants.LISTING_OPTIONS);
			searchListCriteria.removeAllAttribute();
			searchListCriteria.setAttribute(AttributeConstants.USER, new Object[]{user, SearchListCriteria.EQ});
			auctionBidsList = AuctionBidsServiceDelegator.getList(searchListCriteria);
			for (Iterator<AuctionBids> iterator = auctionBidsList.iterator(); iterator.hasNext();) {
				AuctionBids type = (AuctionBids) iterator.next();
				Items items = new Items();
				items = type.getItems();
				AuctionBids auctionBids = new AuctionBids();
				ItemNote itemNote = new ItemNote();
				String[] giftIcon;
				giftIcon = items.getGiftIcon().split(AdminConstants.SEMICOLON);
				if(giftIcon.length > 0)
					items.setGiftIcon(giftIcon[0]);
				if((items.getQuantity() != null) && (items.getSold() != null))
					items.setAvailable(items.getQuantity() - items.getSold());
				auctionBids = AuctionBidsServiceDelegator.getMaxBidder(items.getItemId());
				if(auctionBids != null){
					items.setBidder(auctionBids.getUser());
					items.setWinningBid(auctionBids);
				}else{
					items.setBidder(new User());
					items.setWinningBid(auctionBids);
				}
				if((items.getQuantity() != null) && (items.getSold() != null))
					items.setAvailable(items.getQuantity() - items.getSold());
				if(items.getEndTime().before(DateUtil.getTodaysDate())){
					items.setClosed(true);
				}else{
					items.setClosed(false);
				}
				if(items.getStartTime().before(DateUtil.addDate(items.getStartTime(), Calendar.DATE, listingOptions.getNumberOfDaysForNewListings()))){
					items.setNewIcon(true);
				}else{
					items.setNewIcon(false);
				}
				/* retrieving note */
				searchCriteria.removeAllAttribute();
				searchCriteria.setAttribute(AttributeConstants.ITEMS, items);
				searchCriteria.setAttribute(AttributeConstants.USER, user);
				itemNote = (ItemNote) ((CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemNote.class)).size() > 0 ? CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemNote.class).get(AdminConstants.ZERO_NUMBER) : new ItemNote());
				items.setItemNote(itemNote);
				itemsList.add(items);
			}
		} catch (Exception e) {
			addActionError("Problem in listing bidding.");
		}
	}

	public UserRating getRating(int userId){
		UserRating rating = new UserRating();
		searchCriteria.removeAllAttribute();
		searchCriteria.setAttribute("ratedUser.id", userId);
		List<UserRating> ratingList = (List<UserRating>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, UserRating.class);
		rating = ratingList.size() > 0 ? ratingList.get(0) : null;
		if(rating != null){
			return rating;
		}
		return null;
	}
	public List<Items> getItemsList() {
		return itemsList;
	}

	public void setItemsList(List<Items> itemsList) {
		this.itemsList = itemsList;
	}

	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;

	}

	public ListingOptions getListingOptions() {
		return listingOptions;
	}

	public void setListingOptions(ListingOptions listingOptions) {
		this.listingOptions = listingOptions;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<Items> getCompareItemsList() {
		return compareItemsList;
	}

	public void setCompareItemsList(List<Items> compareItemsList) {
		this.compareItemsList = compareItemsList;
	}

	public void setInvoice(AuctionInvoices invoice) {
		this.invoice = invoice;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public List<ItemOffers> getItemOffersTemp() {
		return itemOffersTemp;
	}

	public void setItemOffersTemp(List<ItemOffers> itemOffersTemp) {
		this.itemOffersTemp = itemOffersTemp;
	}
}
